/**
* All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
*
* PmContractApplicantChangeService
* 项目合同申请人变更
*
* 修改纪录
* 2023-06-11 版本：1.0 wuyuyang 创建。
* @version 版本：1.0
* @author 作者：wuyuyang
* 创建日期：2023-06-11
*/
package com.topscomm.pm.service.impl;

import com.topscomm.basic.exception.MessageException;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.service.ICboDeptService;
import com.topscomm.mms.service.IMmsDeptExtraInfoService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.pm.service.IPmContractApplicantChangeService;
import com.topscomm.pm.service.impl.auto.PmContractApplicantChangeServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.pm.pojo.PmContractApplicantChangeEntity;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service(value = "pmContractApplicantChangeService")
@Scope(value = "singleton")
public class PmContractApplicantChangeService extends PmContractApplicantChangeServiceAuto implements IPmContractApplicantChangeService {
	@Autowired
	private ICboDeptService cboDeptService;
	@Resource
	private IMmsDeptExtraInfoService deptExtraInfoService;

	/**
	 * @author: wuyuyang
	 * @date: 2023-06-11
	 * @param entity
	 * @throws ServiceException
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity){
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(PmContractApplicantChangeEntity.FieldDocno, docNo);
		}
	}
    /**
     * @Description:    判断新申请人是否属于本省
     * @Author:         wuyuyang
     * @Return:         boolean
     * @exception:
     * @Date:           14/6/2023 上午10:00
     * @Version:        1.0
     **/
	@Override
	public boolean queryDeptcode(Map<String,Object> paraMap) {
		String deptcode = ConvertUtil.convertToString(paraMap.get("deptcode"));
		String usercode = ConvertUtil.convertToString(paraMap.get("usercode"));
		if(deptcode.isEmpty() || usercode.isEmpty()){
			throw new MessageException("传入申请人或者申请部门编码为空");
		}
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("b.CODE = '{0}'", usercode);
		List<Map<String, Object>> list = this.queryMapByWhere(sql.toString(), "queryDeptcode");
		if(list.size() == 0){
			throw new MessageException("传入申请人部门获取失败");
		}
		String deptCodeForNewApplicant = ConvertUtil.convertToString(list.get(0).get("code"));
		String deptCommonCode = deptExtraInfoService.getDeptCommonCode(deptcode);
		String deptCommonCodeForNewApplicant = deptExtraInfoService.getDeptCommonCode(deptCodeForNewApplicant);
		if (deptCommonCode.isEmpty() || deptCommonCodeForNewApplicant == null) {
			throw new MessageException("只能交接本省区人员的工作，请确认！");
		}
		if(!deptCommonCode.equals(deptCommonCodeForNewApplicant)) {
			throw new MessageException("只能交接本省区人员的工作，请确认！");
		}
		return true;
	}
}
